
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.List;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author PhongLan
 */
public class DataTask {

    public DataTask(String sName) {
        REC_STORE = sName;
    }
    private RecordStore rs = null;
    private String REC_STORE = "dbTask";
    private RecordEnumeration re;

    private void db(String str) {
        System.err.println("Msg: " + str);
    }

    public void close() {
        try {
            rs.closeRecordStore();
        } catch (Exception e) {
            db(e.toString());
        }
    }

    public void open() throws RecordStoreException {
        try {
            rs = RecordStore.openRecordStore(REC_STORE, true);
            re = rs.enumerateRecords(null, null, true);
            re.keepUpdated(true);
        } catch (RecordStoreException e) {
            db(e.toString());
        }
    }

    public void delete() {
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore(REC_STORE);
            } catch (Exception e) {
                db(e.toString());
            }
        }
    }
    // ID, Name, Time, IDIMp, Icon, Music 

    public void insert(RecordTask recordTask) {
        try {// Write data into an internal byte array
            ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();
            DataOutputStream strmDataType = new DataOutputStream(strmBytes);
            byte[] buffer = null;
            strmDataType.writeUTF(recordTask.sName);
            strmDataType.writeLong(recordTask.lTime);
            strmDataType.writeInt(recordTask.iIDImp);
            strmDataType.writeUTF(recordTask.sIcon);
            strmDataType.writeUTF(recordTask.sMusic);
            strmDataType.flush(); // Clear any buffered data
            buffer = strmBytes.toByteArray(); // Get stream data into byte array and write RecordTask
            rs.addRecord(buffer, 0, buffer.length);
            strmBytes.reset();
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
    }

    // ID, Name, Time, IDIMp, Icon, Music 
    public void update(int iIndex, RecordTask recordTask) {
        try {
            // Write data into an internal byte array
            ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();
            DataOutputStream strmDataType = new DataOutputStream(strmBytes);
            byte[] buffer;
            strmDataType.writeUTF(recordTask.sName);
            strmDataType.writeLong(recordTask.lTime);
            strmDataType.writeInt(recordTask.iIDImp);
            strmDataType.writeUTF(recordTask.sIcon);
            strmDataType.writeUTF(recordTask.sMusic);
            strmDataType.flush(); // Clear any buffered data
            buffer = strmBytes.toByteArray(); // Get stream data into byte array and write RecordTask
            int id = getIDRecordStoreByIndex(iIndex);
            if (id == -1) {
                return;
            }
            rs.setRecord(id, buffer, 0, buffer.length);
            strmBytes.reset();
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
    }
    // ID, Name, Time, IDIMp, Icon, Music 

    public void delete(int iIndex) {
        try {
            int id = getIDRecordStoreByIndex(iIndex);
            if (id == -1) {
                return;
            }
            rs.deleteRecord(id);
        } catch (Exception e) {
            db(e.toString());
        }
    }

    public void deleteAll() {
        while (re.hasNextElement()) {
            try {
                rs.deleteRecord(re.nextRecordId());
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
        re.reset();
    }

    // ID, Name, Time, IDIMp, Icon, Music 
    public long[] selectTimes() {
        long[] arTimes = null;
        try {
            byte[] recData = new byte[50];
            ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);
            DataInputStream strmDataType = new DataInputStream(strmBytes);
            arTimes = new long[rs.getNumRecords()];
            for (int i = 0; i < rs.getNumRecords(); i++) {
                rs.getRecord(i + 1, recData, 0);
                arTimes[i] = strmDataType.readLong();
                strmBytes.reset();
            }
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
        return arTimes;
    }
    // ID, Name, Time, IDIMp, Icon, Music 

    public RecordTask[] selectRecordTasks() {
        RecordTask[] arRecordTasks = null;
        try {
            byte[] recData = new byte[50];
            ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);
            DataInputStream strmDataType = new DataInputStream(strmBytes);
            arRecordTasks = new RecordTask[rs.getNumRecords()];
            //for (int i = 0; i < rs.getNumRecords(); i++) {
            int i = 0;
            while (re.hasNextElement()) {
                arRecordTasks[i] = new RecordTask();
               // arRecordTasks[i].idRecordStore = re.nextRecordId();
                rs.getRecord(re.nextRecordId(), recData, 0);
                arRecordTasks[i].sName = strmDataType.readUTF();
                arRecordTasks[i].lTime = strmDataType.readLong();
                arRecordTasks[i].iIDImp = strmDataType.readInt();
                arRecordTasks[i].sIcon = strmDataType.readUTF();
                arRecordTasks[i].sMusic = strmDataType.readUTF();
                strmBytes.reset();
                i++;
            }
            re.reset();
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
        return arRecordTasks;
    }

    public RecordTask selectRecordTask(int iIndex) {
        RecordTask RecordTask = null;
        try {
            // Careful: Make sure this is big enough!Better yet, test and reallocate if necessary	
            byte[] recData = new byte[50];
            // Read from the specified byte array	
            ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);
            // Read Java data types from the above byte array	
            DataInputStream strmDataType = new DataInputStream(strmBytes);

            RecordTask recordTask = new RecordTask();
            rs.getRecord(iIndex, recData, 0); // Read back the data types

            recordTask.sName = strmDataType.readUTF();
            recordTask.lTime = strmDataType.readLong();
            recordTask.iIDImp = strmDataType.readInt();
            recordTask.sIcon = strmDataType.readUTF();
            recordTask.sMusic = strmDataType.readUTF();
            strmBytes.reset();
            // Reset so read starts at beginning of array 			
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
        return RecordTask;
    }

    public int getIDRecordStoreByIndex(int index) {
        int id = -1;
        try {
            if (index < rs.getNumRecords()) {
                for (int i = 0; i <= index; i++) {
                    if (re.hasNextElement()) {
                        try {
                            id = re.nextRecordId();
                        } catch (InvalidRecordIDException ex) {
                            ex.printStackTrace();
                        }
                    }
                    //re.reset();
                }
                re.reset();
            }
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
        return id;
    }
    //int[] listIDRecordStore;
    //private int getRecordID
}
